package com.linlong.linlongchat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.linlong.linlongchat.domain.po.Record;
import com.linlong.linlongchat.domain.po.Result;
import com.linlong.linlongchat.mapper.RecordMapper;
import com.linlong.linlongchat.service.IRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linlong.linlongchat.utils.ThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 华少
 * @since 2024-09-20
 */
@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements IRecordService {

    @Autowired
    private RecordMapper recordMapper;


    @Override
    public Result<List<String>> getRecordByAccount(String userAccount) {
        return Result.ok(recordMapper.getRecordByAccount(userAccount));
    }

    @Override
    @Transactional
    public Result addRecord(String friendAccount) {
        Record record = new Record();
        record.setFriendAccount(friendAccount);
        Map<String, Object> claim = ThreadLocalUtil.get();
        String userAccount = (String) claim.get("account");
        record.setUserAccount(userAccount);
        record.setCreateTime(LocalDateTime.now());
        record.setUpdateTime(LocalDateTime.now());
        record.setDeleted(0);
        Record targetRecord = new Record();
        BeanUtils.copyProperties(record,targetRecord);
        targetRecord.setUserAccount(friendAccount);
        targetRecord.setFriendAccount(userAccount);

        recordMapper.insert(record);
        recordMapper.insert(targetRecord);
        return Result.ok();
    }

    @Override
    public Result updateTime(String userAccount, String friendAccount) {
        return Result.ok(recordMapper.updateTime(friendAccount, userAccount));
    }

    @Override
    public Result upDeleteStatus(String userAccount, String friendAccount) {
        return Result.ok(recordMapper.upDeleteStatus(userAccount, friendAccount));
    }


}
